<?php
/**
 * @Author: your name
 * @Date:   2017-05-19 16:11:32
 * @Last Modified by:   your name
 * @Last Modified time: 2017-07-11 14:21:06
 */
namespace app\index\controller;
use think\Controller;
class Home extends Controller
{
    protected $base_info_url='';
    protected $all_info_url='';
    protected $address_url='';
    private $token = 'lamidr';
    private $appid = 'wx30f182b53f2d32bb';
    private $AppSecret = 'fada8b5c75a55fadc1272bfa3cbd23ca';

    public function _initialize()
    {
        $this->set_url();
        session('?url') ||  session('url',request()->url());//保存路由来源，授权后跳转
        $this->is_login()  || $this->login();

    }
    //URL
    public function set_url()
    {
            $url='';
            $url.='Location:https://open.weixin.qq.com/connect/oauth2/authorize?appid=';
            $url.=$this->appid;
            $url.='&redirect_uri=';
            $redirect_uri = urlencode("http://lm.lamidr.com/index/home/reg");//授权后跳转的页面
            $url.=$redirect_uri;
            $this->all_info_url=$url.'&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirec';

    }

    /**
     * 判断是否授权注册过
     */
    public function reg()
    {
        $user=db('user');
        $map=array();
        $row=array();
        $info=$this->get_wx_info();
        if(empty($info)){
            return false;
        }
        $map['openid']=$info['openid'];
        $row=$user->where($map)->find();
        if(!empty($row)){
            session('wx_uid',$row['id']);
        }
        else{
            $data['openid']     =   $info['openid'];
            //$data['nickname']   =   emoji_unified_to_html($info['nickname']);
            $data['nickname']   =   $info['nickname'];
            $data['headimgurl'] =   $info['headimgurl'];
            $data['sex']        =   $info['sex'];
            $data['province']   =   $info['province'];
            $data['city']        =   $info['city'];
            $data['create_time'] =   time();
            $wx_uid = $user->insertGetId($data);
            session('wx_uid',$wx_uid);
        }
        $this->redirect(session('url'));
    }
    /**
     * test url  获取授权
     */
    public function login()
    {
        header('Content-Type: text/html; charset=utf-8');
        header($this->all_info_url);
    }
    /**
     * [is_login 判断是否登录]
     * @return boolean [description]
     */
    public function is_login()
    {
         session('wx_uid',1);//测试，正式的话屏蔽
       if(session('?wx_uid')) {
           return 1;
       }
       else{
           return 0;
       }
    }

    /**
     *获取到token
     */
    private function get_oauth_token()
    {
        $access_token='';
        if(isset($_GET['code'])){
        //获取code后第二步
        $data=array();
        $two_url='https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$this->appid.'&secret='.$this->AppSecret.'&code='.$_GET['code'].'&grant_type=authorization_code';
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $two_url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($ch);
        curl_close($ch);
        $jsoninfo = json_decode($output, true);
        $access_token = $jsoninfo["access_token"];
        }
        return $access_token;
    }

    /**
     * 授权后获取用户信息
     */
     private function get_wx_info()
     {
        $data=array();
        if(isset($_GET['code'])){
            //获取code后第二步
            $data=array();
            $two_url='https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$this->appid.'&secret='.$this->AppSecret.'&code='.$_GET['code'].'&grant_type=authorization_code';
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $two_url);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $output = curl_exec($ch);
            curl_close($ch);
            $jsoninfo = json_decode($output, true);

            $refresh_token = $jsoninfo["refresh_token"];
            //获取access_token后第三步
            $three_url='https://api.weixin.qq.com/sns/oauth2/refresh_token?appid='.$this->appid.'&grant_type=refresh_token&refresh_token='.$refresh_token;
            $ch3 = curl_init();
            curl_setopt($ch3, CURLOPT_URL, $three_url);
            curl_setopt($ch3, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch3, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($ch3, CURLOPT_RETURNTRANSFER, 1);
            $output3 = curl_exec($ch3);
            curl_close($ch3);
            $jsoninfo3 = json_decode($output3, true);
            $access_token = $jsoninfo3["access_token"];
            $openid=$jsoninfo3["openid"];
            //获取openid后，第四步
            $four_url="https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openid.'&lang=zh_CN';
            $ch4 = curl_init();
            curl_setopt($ch4, CURLOPT_URL, $four_url);
            curl_setopt($ch4, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch4, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($ch4, CURLOPT_RETURNTRANSFER, 1);
            $output4 = curl_exec($ch4);
            curl_close($ch4);
            $jsoninfo4 = json_decode($output4, true);
            $data['openid']     =   $jsoninfo4['openid'];
            $data['nickname']   =   $jsoninfo4['nickname'];
            $data['headimgurl'] =   $jsoninfo4['headimgurl'];
            $data['sex']        =   $jsoninfo4['sex'];
            $data['province']   =   $jsoninfo4['province'];
            $data['city']       =   $jsoninfo4['city'];
        }
        $data['openid'] || $data=array();
        return $data;
    }

    public function test(){
        echo request()->controller().'/'.request()->action();
    }
}